﻿/*---------------------------------------------------------------------------------
-----------------***DaLianQianYi***-----------------------
-----------------作者：大连仟亿科技------------------------------------------------------
-----------------联系作者：www.qianyikeji.com----------------------------------
-----------------时间：2024/7/15 14:05:12------------------------------------------
----------------------------------------------------------------------------------*/


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Donal.Xms.Common;

namespace Donal.Xms.Web.qianyi.common
{
    public partial class spec_prices_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.spec_prices().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("spec_prices", DTEnums.ActionEnum.View.ToString()); //检查权限
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.spec_prices bll = new BLL.spec_prices();
            Model.spec_prices model = bll.GetModel(_id);

            rblStatus.SelectedValue = model.status.ToString();
            rblIs_goods.SelectedValue = model.is_goods.ToString();

            txtGoods_or_category_txt.Text = new BLL.products_goods().ArticleGetPremiumTitle(model.is_goods, model.goods_product_category_id);
            hidgoods_product_category_id.Value = model.goods_product_category_id.ToString();

            rblIs_user.SelectedValue = model.is_user.ToString();

            txtUser_or_group_txt.Text = new BLL.company().GetPremiumTitle(model.is_user, model.user_or_group_id);
            hidUser_or_group_id.Value = model.user_or_group_id.ToString();

            rblPrice_type.SelectedValue = model.price_type.ToString();
            txtPrice.Text = model.price.ToString();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.spec_prices model = new Model.spec_prices();
            BLL.spec_prices bll = new BLL.spec_prices();

            model.status = int.Parse(rblStatus.SelectedValue);
            model.is_goods = int.Parse(rblIs_goods.SelectedValue);
            model.goods_product_category_id = int.Parse(hidgoods_product_category_id.Value);
            model.is_user = int.Parse(rblIs_user.SelectedValue);
            model.user_or_group_id = int.Parse(hidUser_or_group_id.Value);
            model.price_type = model.is_goods > 0 ? 1 : int.Parse(rblPrice_type.SelectedValue);
            model.price = decimal.Parse(txtPrice.Text);
            model.priority = priority(model.is_goods, model.price_type, model.is_user);

            if (bll.Add(model) > 0)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加差异价格成功！"); //记录日志
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.spec_prices bll = new BLL.spec_prices();
            Model.spec_prices model = bll.GetModel(_id);

            model.status = int.Parse(rblStatus.SelectedValue);
            model.is_goods = int.Parse(rblIs_goods.SelectedValue);
            model.goods_product_category_id = int.Parse(hidgoods_product_category_id.Value);
            model.is_user = int.Parse(rblIs_user.SelectedValue);
            model.user_or_group_id = int.Parse(hidUser_or_group_id.Value);
            model.price_type = model.is_goods > 0 ? 1 : int.Parse(rblPrice_type.SelectedValue);
            model.price = decimal.Parse(txtPrice.Text);
            model.priority = priority(model.is_goods, model.price_type, model.is_user);

            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改差异价格成功！"); //记录日志
                result = true;
            }

            return result;
        }
        #endregion

        /// <summary>
        /// 获取优先级
        /// </summary>
        /// <param name="isGoods">0商品规格 1商品 2品类</param>
        /// <param name="priceType">0 固定价 1百分比</param>
        /// <param name="isUser">0会员 1组别</param>
        /// <returns>优先级 越小越高</returns>
        public int priority(int isGoods, int priceType, int isUser)
        {
            switch (isGoods)
            {
                case 0://商品规格
                    if (priceType == 0)//固定价格
                    {
                        if (isUser == 0)//会员
                        {
                            return 1;//商品--固定价格--会员
                        }
                        else//组别
                        {
                            return 2;//商品--固定价格--组别
                        }
                    }
                    else//百分比
                    {
                        if (isUser == 0)//会员
                        {
                            return 3;//商品--百分比--会员
                        }
                        else//组别
                        {
                            return 4;//商品--百分比--组别
                        }
                    }
                case 1://商品（商品和品类下面不能用固定价格 商品要高一级）
                    if (isUser == 0)//会员
                    {
                        return 5;//品类--百分比--会员
                    }
                    else//组别
                    {
                        return 6;//品类--百分比--组别
                    }
                default://品类（商品和品类下面不能用固定价格 品类要低一级）
                    if (isUser == 0)//会员
                    {
                        return 7;//品类--百分比--会员
                    }
                    else//组别
                    {
                        return 8;//品类--百分比--组别
                    }
            }
        }

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("spec_prices", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("修改差异价格成功！", "spec_prices_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("spec_prices", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("添加差异价格成功！", "spec_prices_list.aspx");
            }
        }

    }
}

